﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OMModel;

namespace OpenMenu
{
    public partial class FrmOrder : Form
    {
        private List<NormalProductInfo> productList;
        private DataTable orderTable = BuildDataTable();
        public FrmOrder()
        {
            InitializeComponent();
            productList = OMUtility.DataAccess.GetProductsFromFile("./Config/products.txt");
            dgvProductList.AutoGenerateColumns = false;
            dgvOrderList.AutoGenerateColumns = false;
            dgvProductList.DataSource = productList;
            dgvOrderList.DataSource = orderTable;
        }

        private void FrmOrder_Load(object sender, EventArgs e)
        {
            OMUtility.CueProvider.SetCue(_tbxProductName,"产品名称");
            OMUtility.CueProvider.SetCue(_tbxProductCost, "产品成本");
            OMUtility.CueProvider.SetCue(_tbxProductPrice, "产品单价");
            OMUtility.CueProvider.SetCue(_tbxProductNeedPoint, "产品所需积分");
            OMUtility.CueProvider.SetCue(_tbxProductAmount, "产品数量");
            OMUtility.CueProvider.SetCue(_tbxDetails, "备注");

            
        }

        private void addOneToolStripMenuItem_Click(object sender, EventArgs e)
        {
           string name = dgvProductList.CurrentRow.Cells[0].Value.ToString();
           NormalProductInfo tmp = null;
           foreach(NormalProductInfo product in productList){
               if (product.Name.Equals(name))
               {
                   tmp = product;
                   break;
               }
           }

           bool flag = false;
           if (orderTable.Rows.Count == 0)
               flag = true;
           foreach(DataRow dr in orderTable.Rows){
               if (dr["Name"].Equals(name))
               {
                   int a = (int)dr["Amount"];
                   a++;
                   dr["Amount"] = a;
                   flag = false;
                   break;
               }
               else
               {
                   flag = true;
               }
           }
           if (flag)
           {
               DataRow row;
               row = orderTable.NewRow();
               row["Id"] = tmp.ProductId;
               row["Type"] = tmp.Type;
               row["Name"] = tmp.Name;
               row["OrderType"] = "PC";
               row["Price"] = tmp.Price;
               row["Point"] = tmp.Points;
               row["Amount"] = tmp.Amount;
               row["Quantity"] = tmp.Quantity;
               row["Taste"] = tmp.Taste;
               row["OrderPerson"] = "admin";
               orderTable.Rows.Add(row);
           }
           
           
        }


        private static DataTable BuildDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(System.String));
            dt.Columns.Add("Type", typeof(System.String));
            dt.Columns.Add("Name", typeof(System.String));
            dt.Columns.Add("OrderType", typeof(System.String));
            dt.Columns.Add("Price", typeof(System.Double));
            dt.Columns.Add("Point", typeof(System.Int32));
            dt.Columns.Add("Amount", typeof(System.Int32));
            dt.Columns.Add("Quantity", typeof(System.String));
            dt.Columns.Add("Taste", typeof(System.String));
            dt.Columns.Add("OrderPerson", typeof(System.String));
            return dt;
        }

        private void minusOneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string name = dgvProductList.CurrentRow.Cells[0].Value.ToString();
            NormalProductInfo tmp = null;
            foreach (NormalProductInfo product in productList)
            {
                if (product.Name.Equals(name))
                {
                    tmp = product;
                    break;
                }
            }
            if (orderTable.Rows.Count == 0)
                return;
            foreach (DataRow dr in orderTable.Rows)
            {
                if (dr["Name"].Equals(name))
                {
                    int a = (int)dr["Amount"];
                    a--;
                    dr["Amount"] = a;
                    break;
                }
            }
        }

    }
}
